﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;
using DotNet.Highcharts;
using DotNet.Highcharts.Enums;
using DotNet.Highcharts.Helpers;
using DotNet.Highcharts.Options;
using FTBS.Models.BLO;
using FTBS.Models.Entities.Concert;
using FTBS.Models.Entities;

namespace FTBS.Controllers
{
    public class ReportController : Controller
    {
        private ReportBLO _reportBLO = new ReportBLO();

        /// <summary>
        /// Supplier Report Page
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Supplier")
            {
                return Redirect("/Home/Announcement");
            }
            //Variable for 2 datetime Picker
            var fromDateValue = DateTime.Today;
            var toDateValue = DateTime.Today;
            //var toDateValue = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month));
            ViewBag.ToDate = toDateValue;
            ViewBag.FromDate = fromDateValue;
            // Set View mode                                                                                    
            List<string> viewMode = new List<string> { "Ngày", "Tháng", "Năm" };
            ViewBag.ViewMode = new SelectList(viewMode);
            return View();
        }

        /// <summary>
        /// Admin Report Page
        /// </summary>
        /// <returns></returns>
        public ActionResult AdminReport()
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Admin")
            {
                return Redirect("/Home/Announcement");
            }
            ViewBag.ListSupplier = new SelectList(_reportBLO.GetSuppliers());
            //Variable for 2 datetime Picker
            var fromDateValue = DateTime.Today;
            var toDateValue = DateTime.Today;
            //var toDateValue = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month));
            ViewBag.ToDate = toDateValue;
            ViewBag.FromDate = fromDateValue;
            // Set View mode                                                                                    
            List<string> viewMode = new List<string> { "Ngày", "Tháng", "Năm" };
            ViewBag.ViewMode = new SelectList(viewMode);
            return View();
        }

        #region Supplier Report View
        /// <summary>
        /// Redirect supplier to a view for each case of month or year or day mode
        /// </summary>
        /// <param name="viewMode"></param>
        /// <param name="fTime"></param>
        /// <param name="tTime"></param>
        /// <returns></returns>
        public ActionResult SupplierReportView(string viewMode, string fTime, string tTime)
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Supplier")
            {
                return Redirect("/Home/Announcement");
            }
            // Specify exactly how to interpret the string.
            IFormatProvider culture = new System.Globalization.CultureInfo("vi-VN", true);
            DateTime fDate = DateTime.Parse(fTime, culture, System.Globalization.DateTimeStyles.AssumeLocal);
            DateTime tDate = DateTime.Parse(tTime, culture, System.Globalization.DateTimeStyles.AssumeLocal);

            if (viewMode == "Ngày")
            {
                return RedirectToAction("DateRangeReportView", new { fDate, tDate });
            }
            if (viewMode == "Tháng")
            {
                return RedirectToAction("YearReportView", new { fDate, tDate });
            }
            if (viewMode == "Năm")
            {
                return RedirectToAction("AllYearsReportView", new { fDate, tDate });
            }
            return View();
        }

        /// <summary>       
        /// view for year-mode-view
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public ActionResult AllYearsReportView(DateTime fDate, DateTime tDate)
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Supplier")
            {
                return Redirect("/Home/Announcement");
            }
            string usn = GetUserName();
            List<ReportTube> fData = _reportBLO.GetSupplierTimeRangeData(usn, fDate, tDate);
            List<ReportTube> categoriesData = _reportBLO.GetSupplierCategoriesRank(usn, fDate, tDate);
            List<ReportTube> theaterSold = _reportBLO.GetSupplierTheatersTimeRangeSoldData(usn, fDate, tDate);

            List<ReportTube> data = SortSystemTheatersDataInYear(fData);
            List<ReportTube> favoriteDayRank = SortSystemDataInDayOfWeek(fData);
            List<ReportTube> favoriteMonthRank = SortSystemDataInMonthOfYear(fData);
            List<ReportTube> sortedTheaterSoldData = SortSystemTheatersDataInYear(theaterSold);
            List<ReportTube> sortedCategoriesData = SortSystemCategoriesDataInMonth(categoriesData);

            ViewBag.TheaterBalance = sortedTheaterSoldData;
            ViewBag.CateRank = sortedCategoriesData;
            ViewBag.DayRank = favoriteDayRank;
            ViewBag.MonthRank = favoriteMonthRank;
            ViewBag.FDate = fDate;
            ViewBag.TDate = tDate;
            return View(data);
        }

        /// <summary>
        /// view for month-mode-view
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public ActionResult YearReportView(DateTime fDate, DateTime tDate)
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Supplier")
            {
                return Redirect("/Home/Announcement");
            }
            string usn = GetUserName();

            List<ReportTube> fData = _reportBLO.GetSupplierTimeRangeData(usn, fDate, tDate);
            List<ReportTube> categoriesData = _reportBLO.GetSupplierCategoriesRank(usn, fDate, tDate);
            List<ReportTube> theaterSold = _reportBLO.GetSupplierTheatersTimeRangeSoldData(usn, fDate, tDate);

            List<ReportTube> data = SortSystemTheatersDataInMonth(fData);
            List<ReportTube> favoriteDayRank = SortSystemDataInDayOfWeek(fData);
            List<ReportTube> favoriteMonthRank = SortSystemDataInMonthOfYear(fData);
            List<ReportTube> sortedTheaterSoldData = SortSystemTheatersDataInMonth(theaterSold);
            List<ReportTube> sortedCategoriesData = SortSystemCategoriesDataInMonth(categoriesData);

            ViewBag.TheaterBalance = sortedTheaterSoldData;
            ViewBag.CateRank = sortedCategoriesData;
            ViewBag.DayRank = favoriteDayRank;
            ViewBag.MonthRank = favoriteMonthRank;
            ViewBag.FDate = fDate;
            ViewBag.TDate = tDate;
            return View(data);
        }

        /// <summary> 
        /// view for day-mode-view
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public ActionResult DateRangeReportView(DateTime fDate, DateTime tDate)
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Supplier")
            {
                return Redirect("/Home/Announcement");
            }
            string usn = GetUserName();
            List<ReportTube> data = _reportBLO.GetSupplierTimeRangeData(usn, fDate, tDate);
            List<ReportTube> categoriesData = _reportBLO.GetSupplierCategoriesRank(usn, fDate, tDate);
            List<ReportTube> theaterSold = _reportBLO.GetSupplierTheatersTimeRangeSoldData(usn, fDate, tDate);

            List<ReportTube> favoriteDayRank = SortSystemDataInDayOfWeek(data);
            List<ReportTube> favoriteMonthRank = SortSystemDataInMonthOfYear(data);

            ViewBag.TheaterBalance = theaterSold;
            ViewBag.CateRank = categoriesData;
            ViewBag.DayRank = favoriteDayRank;
            ViewBag.MonthRank = favoriteMonthRank;
            ViewBag.FDate = fDate;
            ViewBag.TDate = tDate;
            ViewBag.Supplier = usn;

            return View(data);
        }
        #endregion

        #region Admin Report View
        /// <summary>
        /// Redirect admin to a view for each case of month or year or day mode
        /// </summary>
        /// <returns></returns>
        public ActionResult AdminReportView(string usn, string viewMode, string fTime, string tTime)
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Admin")
            {
                return Redirect("/Home/Announcement");
            }
            // Specify exactly how to interpret the string.
            IFormatProvider culture = new System.Globalization.CultureInfo("vi-VN", true);
            DateTime fDate = DateTime.Parse(fTime, culture, System.Globalization.DateTimeStyles.AssumeLocal);
            DateTime tDate = DateTime.Parse(tTime, culture, System.Globalization.DateTimeStyles.AssumeLocal);
            if (viewMode == "Ngày")
            {
                return RedirectToAction("DateRangeAdminReportView", new { usn, fDate, tDate });
            }
            if (viewMode == "Tháng")
            {
                return RedirectToAction("YearAdminReportView", new { usn, fDate, tDate });
            }
            if (viewMode == "Năm")
            {
                return RedirectToAction("AllYearsAdminReportView", new { usn, fDate, tDate });
            }
            return View();
        }

        /// <summary>
        /// view for year-mode-view
        /// </summary>
        /// <param name="supplier"></param>
        /// <returns></returns>
        public ActionResult AllYearsAdminReportView(string usn, DateTime fDate, DateTime tDate)
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Admin")
            {
                return Redirect("/Home/Announcement");
            }
            List<ReportTube> data = new List<ReportTube>();

            if (usn == "0" || usn == null)
            {
                List<ReportTube>  fData = _reportBLO.GetSystemSuppliersTimeRangeData(fDate, tDate);
                List<ReportTube> sData = _reportBLO.GetSystemSuppliersTimeRangeSoldData(fDate, tDate);
                List<ReportTube> theaterData = _reportBLO.GetSystemTheatersTimeRangeData(fDate, tDate);
                List<ReportTube> theaterSold = _reportBLO.GetSystemTheatersTimeRangeSoldData(fDate, tDate);
                List<ReportTube> categoriesData = _reportBLO.GetSystemCategoriesRank(fDate, tDate);

                data = SortSystemDataInYear(fData);
                List<ReportTube> favoriteDayRank = SortSystemDataInDayOfWeek(fData);
                List<ReportTube> favoriteMonthRank = SortSystemDataInMonthOfYear(fData);
                List<ReportTube> theaterSortedData = SortSystemTheatersDataInYear(theaterData);
                List<ReportTube> sortedCategoriesData = SortSystemCategoriesDataInYear(categoriesData);
                List<ReportTube> systemBalance = SortSystemDataInYear(sData);
                List<ReportTube> sortedTheaterSoldData = SortSystemTheatersDataInYear(theaterSold);

                ViewBag.TheaterData = theaterSortedData;
                ViewBag.CateRank = sortedCategoriesData;
                ViewBag.DayRank = favoriteDayRank;
                ViewBag.MonthRank = favoriteMonthRank;
                ViewBag.SystemBalance = systemBalance;
                ViewBag.TheaterBalance = sortedTheaterSoldData;
                ViewBag.mode = 0;    
            }
            else
            {
                List<ReportTube>  fData = _reportBLO.GetSupplierTimeRangeData(usn, fDate, tDate);
                List<ReportTube> categoriesData = _reportBLO.GetSupplierCategoriesRank(usn, fDate, tDate);
                List<ReportTube> theaterSold = _reportBLO.GetSupplierTheatersTimeRangeSoldData(usn, fDate, tDate);

                data = SortSystemTheatersDataInYear(fData);
                List<ReportTube> favoriteDayRank = SortSystemDataInDayOfWeek(fData);
                List<ReportTube> favoriteMonthRank = SortSystemDataInMonthOfYear(fData);
                List<ReportTube> sortedTheaterSoldData = SortSystemTheatersDataInYear(theaterSold);
                List<ReportTube> sortedCategoriesData = SortSystemCategoriesDataInMonth(categoriesData);

                ViewBag.TheaterBalance = sortedTheaterSoldData;
                ViewBag.CateRank = sortedCategoriesData;
                ViewBag.DayRank = favoriteDayRank;
                ViewBag.MonthRank = favoriteMonthRank;
                ViewBag.mode = 1;
            }
            //============================================
            
            ViewBag.Supplier = usn;
            ViewBag.FDate = fDate;
            ViewBag.TDate = tDate;
            return View(data);
        }

        /// <summary>
        ///  view for month-mode-view
        /// </summary>
        /// <param name="supplier"></param>
        /// <param name="year"></param>
        /// <returns></returns>
        public ActionResult YearAdminReportView(string usn, DateTime fDate, DateTime tDate)
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Admin")
            {
                return Redirect("/Home/Announcement");
            }
            List<ReportTube> data = new List<ReportTube>();
            if (usn == "0" || usn == null)
            {
                List<ReportTube> fData = _reportBLO.GetSystemSuppliersTimeRangeData(fDate, tDate);
                List<ReportTube> sData = _reportBLO.GetSystemSuppliersTimeRangeSoldData(fDate, tDate);
                List<ReportTube> theaterData = _reportBLO.GetSystemTheatersTimeRangeData(fDate, tDate);
                List<ReportTube> theaterSold = _reportBLO.GetSystemTheatersTimeRangeSoldData(fDate, tDate);
                List<ReportTube> categoriesData = _reportBLO.GetSystemCategoriesRank(fDate, tDate);

                data = SortSystemDataInMonth(fData);
                List<ReportTube> systemBalance = SortSystemDataInYear(sData);
                List<ReportTube> favoriteDayRank = SortSystemDataInDayOfWeek(fData);
                List<ReportTube> favoriteMonthRank = SortSystemDataInMonthOfYear(fData);
                List<ReportTube> theaterSortedData = SortSystemTheatersDataInMonth(theaterData);
                List<ReportTube> sortedTheaterSoldData = SortSystemTheatersDataInMonth(theaterSold);
                List<ReportTube> sortedCategoriesData = SortSystemCategoriesDataInMonth(categoriesData);

                ViewBag.SystemBalance = systemBalance;
                ViewBag.TheaterBalance = sortedTheaterSoldData;
                ViewBag.TheaterData = theaterSortedData;
                ViewBag.CateRank = sortedCategoriesData;
                ViewBag.DayRank = favoriteDayRank;
                ViewBag.MonthRank = favoriteMonthRank;
                ViewBag.mode = 0;
            }
            else
            {
                List<ReportTube> fData = _reportBLO.GetSupplierTimeRangeData(usn, fDate, tDate);
                List<ReportTube> categoriesData = _reportBLO.GetSupplierCategoriesRank(usn, fDate, tDate);
                List<ReportTube> theaterSold = _reportBLO.GetSupplierTheatersTimeRangeSoldData(usn, fDate, tDate);

                data = SortSystemTheatersDataInMonth(fData);
                List<ReportTube> favoriteDayRank = SortSystemDataInDayOfWeek(fData);
                List<ReportTube> favoriteMonthRank = SortSystemDataInMonthOfYear(fData);
                List<ReportTube> sortedTheaterSoldData = SortSystemTheatersDataInMonth(theaterSold);
                List<ReportTube> sortedCategoriesData = SortSystemCategoriesDataInMonth(categoriesData);

                ViewBag.TheaterBalance = sortedTheaterSoldData;
                ViewBag.CateRank = sortedCategoriesData;
                ViewBag.DayRank = favoriteDayRank;
                ViewBag.MonthRank = favoriteMonthRank;
                ViewBag.mode = 1;
            }
            ViewBag.Supplier = usn;
            ViewBag.FDate = fDate;
            ViewBag.TDate = tDate;
            return View(data);
        }

        /// <summary>
        /// view for day-mode-view
        /// </summary>
        /// <param name="supplier"></param>
        /// <param name="fday"></param>
        /// <param name="tday"></param>
        /// <param name="month"></param>
        /// <param name="year"></param>
        /// <returns></returns>
        public ActionResult DateRangeAdminReportView(string usn, DateTime fDate, DateTime tDate)
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Admin")
            {
                return Redirect("/Home/Announcement");
            }
            List<ReportTube> data = new List<ReportTube>();
            if (usn == "0" || usn == null)
            {
                data = _reportBLO.GetSystemSuppliersTimeRangeData(fDate, tDate);
                List<ReportTube> categoriesData = _reportBLO.GetSystemCategoriesRank(fDate, tDate);
                List<ReportTube> theaterSortedData = _reportBLO.GetSystemTheatersTimeRangeData(fDate, tDate);
                List<ReportTube> soldData = _reportBLO.GetSystemSuppliersTimeRangeSoldData(fDate, tDate);
                List<ReportTube> theaterSold = _reportBLO.GetSystemTheatersTimeRangeSoldData(fDate, tDate);

                List<ReportTube> favoriteDayRank = SortSystemDataInDayOfWeek(data);
                List<ReportTube> favoriteMonthRank = SortSystemDataInMonthOfYear(data);

                ViewBag.SystemBalance = GroupDataInSupplier(soldData);
                ViewBag.TheaterBalance = GroupDataInTheater(theaterSold);
                ViewBag.TheaterData = theaterSortedData;
                ViewBag.CateRank = categoriesData;
                ViewBag.DayRank = favoriteDayRank;
                ViewBag.MonthRank = favoriteMonthRank;
                ViewBag.mode = 0;
            }
            else
            {
                data = _reportBLO.GetSupplierTimeRangeData(usn, fDate, tDate);
                List<ReportTube> categoriesData = _reportBLO.GetSupplierCategoriesRank(usn, fDate, tDate);
                List<ReportTube> theaterSold = _reportBLO.GetSupplierTheatersTimeRangeSoldData(usn, fDate, tDate);

                List<ReportTube> favoriteDayRank = SortSystemDataInDayOfWeek(data);
                List<ReportTube> favoriteMonthRank = SortSystemDataInMonthOfYear(data);

                ViewBag.TheaterBalance = GroupDataInTheater(theaterSold);
                ViewBag.CateRank = categoriesData;
                ViewBag.DayRank = favoriteDayRank;
                ViewBag.MonthRank = favoriteMonthRank;
                ViewBag.mode = 1;
            }
            ViewBag.Supplier = usn;
            ViewBag.FDate = fDate;
            ViewBag.TDate = tDate;
            return View(data);
        }
        #endregion

        #region Detail Page
        //Admin view
        public ActionResult SystemSupplierDaysDetail(string usn, string fTime, string tTime, string sortOrder, string currentFilter, string searchString, int? page, bool? isSortDescending, string firstFromTime, string firstToTime)
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Admin")
            {
                return Redirect("/Home/Announcement");
            }
            // Specify exactly how to interpret the string.
            IFormatProvider culture = new System.Globalization.CultureInfo("en-US", true);

            var arrayFromDate = fTime.Split('/');
            int fromDay = Convert.ToInt32(arrayFromDate[0]);
            int fromMonth = Convert.ToInt32(arrayFromDate[1]);
            int fromYear = Convert.ToInt32(arrayFromDate[2]);
            DateTime fromDateValue = new DateTime(fromYear, fromMonth, fromDay);

            var arrayToDate = tTime.Split('/');
            int toDay = Convert.ToInt32(arrayToDate[0]);
            int toMonth = Convert.ToInt32(arrayToDate[1]);
            int toYear = Convert.ToInt32(arrayToDate[2]);
            DateTime toDateValue = new DateTime(toYear, toMonth, toDay);

            var arrayFirstFromDate = firstFromTime.Split('/');
            int firstFromDay = Convert.ToInt32(arrayFirstFromDate[0]);
            int firstfromMonth = Convert.ToInt32(arrayFirstFromDate[1]);
            int firstFromYear = Convert.ToInt32(arrayFirstFromDate[2]);
            DateTime firstFromDateValue = new DateTime(firstFromYear, firstfromMonth, firstFromDay);
            ViewBag.FirstFromDate = firstFromDateValue;

            var arrayFirstToDate = firstToTime.Split('/');
            int firstToDay = Convert.ToInt32(arrayFirstToDate[0]);
            int firstToMonth = Convert.ToInt32(arrayFirstToDate[1]);
            int firstToYear = Convert.ToInt32(arrayFirstToDate[2]);
            DateTime firstToDateValue = new DateTime(firstToYear, firstToMonth, firstToDay);
            ViewBag.FirstToDate = firstToDateValue;   

            //wrong datetime inputs => return view old view
            bool isDateFalse = false;
            if (fromDateValue > toDateValue)
            {
                isDateFalse = true;
                fromDateValue = firstFromDateValue;
                toDateValue = firstToDateValue;
                ViewBag.Alert = isDateFalse;
            }
            ViewBag.Alert = isDateFalse;

            //DateTime fDate = DateTime.Parse(fromDateValue);
            //DateTime tDate = DateTime.Parse(toDateValue);
            List<ReportTube> data = _reportBLO.GetSupplierTheatersTimeRangeSoldData(usn, fromDateValue, toDateValue).OrderBy(theater => theater.TheaterName).ToList();

            //calculate total resource of data
            List<ReportTube> tempData = _reportBLO.GetSupplierTheatersTimeRangeSoldData(usn, fromDateValue, toDateValue).OrderBy(theater => theater.TheaterName).ToList();
            double totalBalance = 0;
            double totalRevenue = 0;
            int totalNumBalance = 0;
            foreach (var item in tempData)
            {
                //calculating balance in each tube
                totalBalance = totalBalance + item.Balance;
                totalRevenue = totalRevenue + item.TotalPrice;
                totalNumBalance = totalNumBalance + item.NumberOfSoldTicket;
            }
            ViewBag.TotalBalance = totalBalance;
            ViewBag.TotalRevenue = totalRevenue;
            ViewBag.TotalNumBalance = totalNumBalance;

            //===================Paging +sorting Model===================
            ViewBag.NameSortParm = "theater_desc";
            ViewBag.CurrentSort = sortOrder;

            if (searchString != null)
            {
                page = 1;
            }
            else
            {
                searchString = currentFilter;
            }

            ViewBag.CurrentFilter = searchString;
            ViewBag.IsSortDes = false;

            if (!String.IsNullOrEmpty(searchString))
            {
                data = data.Where(theater => theater.TheaterName.ToUpper().Contains(searchString.ToUpper())
                    || theater.ReportDate.ToShortDateString().ToUpper().Contains(searchString.ToUpper())
                                                  ).ToList();
            }
            switch (sortOrder)
            {
                case "theater_desc":
                    if (isSortDescending == true)
                    {
                        data = data.OrderByDescending(theater => theater.TheaterName).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        data = data.OrderBy(theater => theater.TheaterName).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                case "date_desc":
                    if (isSortDescending == true)
                    {
                        data = data.OrderByDescending(theater => theater.ReportDate).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        data = data.OrderBy(theater => theater.ReportDate).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                case "sold_ticket_desc":
                    if (isSortDescending == true)
                    {
                        data = data.OrderByDescending(theater => theater.NumberOfSoldTicket).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        data = data.OrderBy(theater => theater.NumberOfSoldTicket).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                case "revenue_desc":
                    if (isSortDescending == true)
                    {
                        data = data.OrderByDescending(theater => theater.TotalPrice).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        data = data.OrderBy(theater => theater.TotalPrice).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                case "balance_desc":
                    if (isSortDescending == true)
                    {
                        data = data.OrderByDescending(theater => (theater.TotalPrice - theater.TotalPurePrice)).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        data = data.OrderBy(theater => (theater.TotalPrice - theater.TotalPurePrice)).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                default:
                    data = data.OrderBy(theater => theater.TheaterName).ToList();
                    ViewBag.IsSortDes = false;
                    break;
            }

            int pageSize = 9;
            int pageNumber = (page ?? 1);
            ViewBag.Supplier = usn;
            ViewBag.FDate = fromDateValue;
            ViewBag.TDate = toDateValue;
            ViewBag.DataSize = tempData.Count;

            return View(data.ToPagedList(pageNumber, pageSize));
        }
        public ActionResult SystemSuppliersMonthsDetail(string usn, string fTime, string tTime, string sortOrder, string currentFilter, string searchString, int? page, bool? isSortDescending, string firstFromTime, string firstToTime)
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Admin")
            {
                return Redirect("/Home/Announcement");
            }
            // Specify exactly how to interpret the string.
            IFormatProvider culture = new System.Globalization.CultureInfo("en-US", true);

            var arrayFromDate = fTime.Split('/');
            int fromDay = Convert.ToInt32(arrayFromDate[0]);
            int fromMonth = Convert.ToInt32(arrayFromDate[1]);
            int fromYear = Convert.ToInt32(arrayFromDate[2]);
            DateTime fromDateValue = new DateTime(fromYear, fromMonth, fromDay);

            var arrayToDate = tTime.Split('/');
            int toDay = Convert.ToInt32(arrayToDate[0]);
            int toMonth = Convert.ToInt32(arrayToDate[1]);
            int toYear = Convert.ToInt32(arrayToDate[2]);
            DateTime toDateValue = new DateTime(toYear, toMonth, toDay);

            var arrayFirstFromDate = firstFromTime.Split('/');
            int firstFromDay = Convert.ToInt32(arrayFirstFromDate[0]);
            int firstfromMonth = Convert.ToInt32(arrayFirstFromDate[1]);
            int firstFromYear = Convert.ToInt32(arrayFirstFromDate[2]);
            DateTime firstFromDateValue = new DateTime(firstFromYear, firstfromMonth, firstFromDay);
            ViewBag.FirstFromDate = firstFromDateValue;

            var arrayFirstToDate = firstToTime.Split('/');
            int firstToDay = Convert.ToInt32(arrayFirstToDate[0]);
            int firstToMonth = Convert.ToInt32(arrayFirstToDate[1]);
            int firstToYear = Convert.ToInt32(arrayFirstToDate[2]);
            DateTime firstToDateValue = new DateTime(firstToYear, firstToMonth, firstToDay);
            ViewBag.FirstToDate = firstToDateValue;

            //wrong datetime inputs => return view old view
            bool isDateFalse = false;
            if (fromDateValue > toDateValue)
            {
                isDateFalse = true;
                fromDateValue = firstFromDateValue;
                toDateValue = firstToDateValue;
                ViewBag.Alert = isDateFalse;
            }
            ViewBag.Alert = isDateFalse;

            //DateTime fDate = DateTime.Parse(fromDateValue);
            //DateTime tDate = DateTime.Parse(toDateValue);
            List<ReportTube> preData = _reportBLO.GetSupplierTheatersTimeRangeSoldData(usn, fromDateValue, toDateValue).OrderBy(theater => theater.TheaterName).ToList();
            List<ReportTube> data = SortSystemDataInMonth(preData);
            //calculate total resource of data
            List<ReportTube> tempData = _reportBLO.GetSupplierTheatersTimeRangeSoldData(usn, fromDateValue, toDateValue).OrderBy(theater => theater.TheaterName).ToList();
            double totalBalance = 0;
            double totalRevenue = 0;
            int totalNumBalance = 0;
            foreach (var item in tempData)
            {
                //calculating balance in each tube
                totalBalance = totalBalance + item.Balance;
                totalRevenue = totalRevenue + item.TotalPrice;
                totalNumBalance = totalNumBalance + item.NumberOfSoldTicket;
            }
            ViewBag.TotalBalance = totalBalance;
            ViewBag.TotalRevenue = totalRevenue;
            ViewBag.TotalNumBalance = totalNumBalance;

            //===================Paging +sorting Model===================
            ViewBag.NameSortParm = "theater_desc";
            ViewBag.CurrentSort = sortOrder;

            if (searchString != null)
            {
                page = 1;
            }
            else
            {
                searchString = currentFilter;
            }

            ViewBag.CurrentFilter = searchString;
            ViewBag.IsSortDes = false;

            if (!String.IsNullOrEmpty(searchString))
            {
                data = data.Where(theater => theater.TheaterName.ToUpper().Contains(searchString.ToUpper())
                    || theater.ReportDate.ToShortDateString().ToUpper().Contains(searchString.ToUpper())
                                                  ).ToList();
            }
            switch (sortOrder)
            {
                case "theater_desc":
                    if (isSortDescending == true)
                    {
                        data = data.OrderByDescending(theater => theater.TheaterName).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        data = data.OrderBy(theater => theater.TheaterName).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                case "date_desc":
                    if (isSortDescending == true)
                    {
                        data = data.OrderByDescending(theater => theater.ReportDate).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        data = data.OrderBy(theater => theater.ReportDate).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                case "sold_ticket_desc":
                    if (isSortDescending == true)
                    {
                        data = data.OrderByDescending(theater => theater.NumberOfSoldTicket).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        data = data.OrderBy(theater => theater.NumberOfSoldTicket).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                case "revenue_desc":
                    if (isSortDescending == true)
                    {
                        data = data.OrderByDescending(theater => theater.TotalPrice).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        data = data.OrderBy(theater => theater.TotalPrice).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                case "balance_desc":
                    if (isSortDescending == true)
                    {
                        data = data.OrderByDescending(theater => (theater.TotalPrice - theater.TotalPurePrice)).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        data = data.OrderBy(theater => (theater.TotalPrice - theater.TotalPurePrice)).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                default:
                    data = data.OrderBy(theater => theater.TheaterName).ToList();
                    ViewBag.IsSortDes = false;
                    break;
            }

            int pageSize = 9;
            int pageNumber = (page ?? 1);
            ViewBag.Supplier = usn;
            ViewBag.FDate = fromDateValue;
            ViewBag.TDate = toDateValue;
            ViewBag.DataSize = tempData.Count;

            return View(data.ToPagedList(pageNumber, pageSize));
        }
        public ActionResult SystemSuppliersYearsDetail(string usn, string fTime, string tTime, string sortOrder, string currentFilter, string searchString, int? page, bool? isSortDescending, string firstFromTime, string firstToTime)
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Admin")
            {
                return Redirect("/Home/Announcement");
            }
            // Specify exactly how to interpret the string.
            IFormatProvider culture = new System.Globalization.CultureInfo("en-US", true);

            var arrayFromDate = fTime.Split('/');
            int fromDay = Convert.ToInt32(arrayFromDate[0]);
            int fromMonth = Convert.ToInt32(arrayFromDate[1]);
            int fromYear = Convert.ToInt32(arrayFromDate[2]);
            DateTime fromDateValue = new DateTime(fromYear, fromMonth, fromDay);

            var arrayToDate = tTime.Split('/');
            int toDay = Convert.ToInt32(arrayToDate[0]);
            int toMonth = Convert.ToInt32(arrayToDate[1]);
            int toYear = Convert.ToInt32(arrayToDate[2]);
            DateTime toDateValue = new DateTime(toYear, toMonth, toDay);

            var arrayFirstFromDate = firstFromTime.Split('/');
            int firstFromDay = Convert.ToInt32(arrayFirstFromDate[0]);
            int firstfromMonth = Convert.ToInt32(arrayFirstFromDate[1]);
            int firstFromYear = Convert.ToInt32(arrayFirstFromDate[2]);
            DateTime firstFromDateValue = new DateTime(firstFromYear, firstfromMonth, firstFromDay);
            ViewBag.FirstFromDate = firstFromDateValue;

            var arrayFirstToDate = firstToTime.Split('/');
            int firstToDay = Convert.ToInt32(arrayFirstToDate[0]);
            int firstToMonth = Convert.ToInt32(arrayFirstToDate[1]);
            int firstToYear = Convert.ToInt32(arrayFirstToDate[2]);
            DateTime firstToDateValue = new DateTime(firstToYear, firstToMonth, firstToDay);
            ViewBag.FirstToDate = firstToDateValue;

            //wrong datetime inputs => return view old view
            bool isDateFalse = false;
            if (fromDateValue > toDateValue)
            {
                isDateFalse = true;
                fromDateValue = firstFromDateValue;
                toDateValue = firstToDateValue;
                ViewBag.Alert = isDateFalse;
            }
            ViewBag.Alert = isDateFalse;

            //DateTime fDate = DateTime.Parse(fromDateValue);
            //DateTime tDate = DateTime.Parse(toDateValue);
            List<ReportTube> preData = _reportBLO.GetSupplierTheatersTimeRangeSoldData(usn, fromDateValue, toDateValue).OrderBy(theater => theater.TheaterName).ToList();
            List<ReportTube> data = SortSystemDataInYear(preData);
            //calculate total resource of data
            List<ReportTube> tempData = _reportBLO.GetSupplierTheatersTimeRangeSoldData(usn, fromDateValue, toDateValue).OrderBy(theater => theater.TheaterName).ToList();
            double totalBalance = 0;
            double totalRevenue = 0;
            int totalNumBalance = 0;
            foreach (var item in tempData)
            {
                //calculating balance in each tube
                totalBalance = totalBalance + item.Balance;
                totalRevenue = totalRevenue + item.TotalPrice;
                totalNumBalance = totalNumBalance + item.NumberOfSoldTicket;
            }
            ViewBag.TotalBalance = totalBalance;
            ViewBag.TotalRevenue = totalRevenue;
            ViewBag.TotalNumBalance = totalNumBalance;
            //===================Paging +sorting Model===================
            ViewBag.NameSortParm = "theater_desc";
            ViewBag.CurrentSort = sortOrder;

            if (searchString != null)
            {
                page = 1;
            }
            else
            {
                searchString = currentFilter;
            }

            ViewBag.CurrentFilter = searchString;
            ViewBag.IsSortDes = false;

            if (!String.IsNullOrEmpty(searchString))
            {
                data = data.Where(theater => theater.TheaterName.ToUpper().Contains(searchString.ToUpper())
                    || theater.ReportDate.ToShortDateString().ToUpper().Contains(searchString.ToUpper())
                                                  ).ToList();
            }
            switch (sortOrder)
            {
                case "theater_desc":
                    if (isSortDescending == true)
                    {
                        data = data.OrderByDescending(theater => theater.TheaterName).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        data = data.OrderBy(theater => theater.TheaterName).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                case "date_desc":
                    if (isSortDescending == true)
                    {
                        data = data.OrderByDescending(theater => theater.ReportDate).ThenBy(theater => theater.TheaterName).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        data = data.OrderBy(theater => theater.ReportDate).ThenBy(theater => theater.TheaterName).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                case "sold_ticket_desc":
                    if (isSortDescending == true)
                    {
                        data = data.OrderByDescending(theater => theater.NumberOfSoldTicket).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        data = data.OrderBy(theater => theater.NumberOfSoldTicket).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                case "revenue_desc":
                    if (isSortDescending == true)
                    {
                        data = data.OrderByDescending(theater => theater.TotalPrice).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        data = data.OrderBy(theater => theater.TotalPrice).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                case "balance_desc":
                    if (isSortDescending == true)
                    {
                        data = data.OrderByDescending(theater => (theater.TotalPrice - theater.TotalPurePrice)).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        data = data.OrderBy(theater => (theater.TotalPrice - theater.TotalPurePrice)).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                default:
                    data = data.OrderBy(theater => theater.TheaterName).ToList();
                    ViewBag.IsSortDes = false;
                    break;
            }

            int pageSize = 9;
            int pageNumber = (page ?? 1);
            ViewBag.Supplier = usn;
            ViewBag.FDate = fromDateValue;
            ViewBag.TDate = toDateValue;
            ViewBag.DataSize = tempData.Count;

            return View(data.ToPagedList(pageNumber, pageSize));
        }

        public ActionResult SystemTheaterDaysDetail(string usn, int theaterId, string fTime, string tTime, string sortOrder, string currentFilter, string searchString, int? page, bool? isSortDescending, string firstFromTime, string firstToTime)
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Admin")
            {
                return Redirect("/Home/Announcement");
            }
            // Specify exactly how to interpret the string.
            IFormatProvider culture = new System.Globalization.CultureInfo("en-US", true);

            var arrayFromDate = fTime.Split('/');
            int fromDay = Convert.ToInt32(arrayFromDate[0]);
            int fromMonth = Convert.ToInt32(arrayFromDate[1]);
            int fromYear = Convert.ToInt32(arrayFromDate[2]);
            DateTime fromDateValue = new DateTime(fromYear, fromMonth, fromDay);

            var arrayToDate = tTime.Split('/');
            int toDay = Convert.ToInt32(arrayToDate[0]);
            int toMonth = Convert.ToInt32(arrayToDate[1]);
            int toYear = Convert.ToInt32(arrayToDate[2]);
            DateTime toDateValue = new DateTime(toYear, toMonth, toDay);

            var arrayFirstFromDate = firstFromTime.Split('/');
            int firstFromDay = Convert.ToInt32(arrayFirstFromDate[0]);
            int firstfromMonth = Convert.ToInt32(arrayFirstFromDate[1]);
            int firstFromYear = Convert.ToInt32(arrayFirstFromDate[2]);
            DateTime firstFromDateValue = new DateTime(firstFromYear, firstfromMonth, firstFromDay);
            ViewBag.FirstFromDate = firstFromDateValue;

            var arrayFirstToDate = firstToTime.Split('/');
            int firstToDay = Convert.ToInt32(arrayFirstToDate[0]);
            int firstToMonth = Convert.ToInt32(arrayFirstToDate[1]);
            int firstToYear = Convert.ToInt32(arrayFirstToDate[2]);
            DateTime firstToDateValue = new DateTime(firstToYear, firstToMonth, firstToDay);
            ViewBag.FirstToDate = firstToDateValue;

            //wrong datetime inputs => return view old view
            bool isDateFalse = false;
            if (fromDateValue > toDateValue)
            {
                isDateFalse = true;
                fromDateValue = firstFromDateValue;
                toDateValue = firstToDateValue;
                ViewBag.Alert = isDateFalse;
            }
            ViewBag.Alert = isDateFalse;

            //DateTime fDate = DateTime.Parse(fromDateValue);
            //DateTime tDate = DateTime.Parse(toDateValue);
            List<ReportTube> data = _reportBLO.GetATheaterData(theaterId, fromDateValue, toDateValue).OrderBy(theater => theater.ReportDate).ToList();
            //calculate total resource of data
            List<ReportTube> tempData = _reportBLO.GetATheaterData(theaterId, fromDateValue, toDateValue).OrderBy(theater => theater.TheaterName).ToList();
            double totalBalance = 0;
            double totalRevenue = 0;
            int totalNumBalance = 0;
            foreach (var item in tempData)
            {
                //calculating balance in each tube
                totalBalance = totalBalance + item.Balance;
                totalRevenue = totalRevenue + item.TotalPrice;
                totalNumBalance = totalNumBalance + item.NumberOfSoldTicket;
            }
            ViewBag.TotalBalance = totalBalance;
            ViewBag.TotalRevenue = totalRevenue;
            ViewBag.TotalNumBalance = totalNumBalance;
            //===================Paging +sorting Model===================
            ViewBag.NameSortParm = "date_desc";
            ViewBag.CurrentSort = sortOrder;

            if (searchString != null)
            {
                page = 1;
            }
            else
            {
                searchString = currentFilter;
            }

            ViewBag.CurrentFilter = searchString;
            ViewBag.IsSortDes = false;

            if (!String.IsNullOrEmpty(searchString))
            {
                data = data.Where(theater => theater.TheaterName.ToUpper().Contains(searchString.ToUpper())
                    || theater.ReportDate.ToShortDateString().ToUpper().Contains(searchString.ToUpper())
                                                  ).ToList();
            }
            switch (sortOrder)
            {
                case "date_desc":
                    if (isSortDescending == true)
                    {
                        data = data.OrderByDescending(theater => theater.ReportDate).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        data = data.OrderBy(theater => theater.ReportDate).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                case "sold_ticket_desc":
                    if (isSortDescending == true)
                    {
                        data = data.OrderByDescending(theater => theater.NumberOfSoldTicket).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        data = data.OrderBy(theater => theater.NumberOfSoldTicket).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                case "revenue_desc":
                    if (isSortDescending == true)
                    {
                        data = data.OrderByDescending(theater => theater.TotalPrice).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        data = data.OrderBy(theater => theater.TotalPrice).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                case "balance_desc":
                    if (isSortDescending == true)
                    {
                        data = data.OrderByDescending(theater => (theater.TotalPrice - theater.TotalPurePrice)).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        data = data.OrderBy(theater => (theater.TotalPrice - theater.TotalPurePrice)).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                default:
                    data = data.OrderBy(theater => theater.ReportDate).ToList();
                    ViewBag.IsSortDes = false;
                    break;
            }

            int pageSize = 9;
            int pageNumber = (page ?? 1);
            ViewBag.Theater = theaterId;
            ViewBag.FDate = fromDateValue;
            ViewBag.TDate = toDateValue;
            ViewBag.TheaterId = theaterId;
            ViewBag.Supplier = usn;
            ViewBag.DataSize = tempData.Count;
            return View(data.ToPagedList(pageNumber, pageSize));
        }
        public ActionResult SystemTheaterMonthsDetail(string usn, int theaterId, string fTime, string tTime, string sortOrder, string currentFilter, string searchString, int? page, bool? isSortDescending, string firstFromTime, string firstToTime)
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Admin")
            {
                return Redirect("/Home/Announcement");
            }
            // Specify exactly how to interpret the string.
            IFormatProvider culture = new System.Globalization.CultureInfo("en-US", true);

            var arrayFromDate = fTime.Split('/');
            int fromDay = Convert.ToInt32(arrayFromDate[0]);
            int fromMonth = Convert.ToInt32(arrayFromDate[1]);
            int fromYear = Convert.ToInt32(arrayFromDate[2]);
            DateTime fromDateValue = new DateTime(fromYear, fromMonth, fromDay);

            var arrayToDate = tTime.Split('/');
            int toDay = Convert.ToInt32(arrayToDate[0]);
            int toMonth = Convert.ToInt32(arrayToDate[1]);
            int toYear = Convert.ToInt32(arrayToDate[2]);
            DateTime toDateValue = new DateTime(toYear, toMonth, toDay);

            var arrayFirstFromDate = firstFromTime.Split('/');
            int firstFromDay = Convert.ToInt32(arrayFirstFromDate[0]);
            int firstfromMonth = Convert.ToInt32(arrayFirstFromDate[1]);
            int firstFromYear = Convert.ToInt32(arrayFirstFromDate[2]);
            DateTime firstFromDateValue = new DateTime(firstFromYear, firstfromMonth, firstFromDay);
            ViewBag.FirstFromDate = firstFromDateValue;

            var arrayFirstToDate = firstToTime.Split('/');
            int firstToDay = Convert.ToInt32(arrayFirstToDate[0]);
            int firstToMonth = Convert.ToInt32(arrayFirstToDate[1]);
            int firstToYear = Convert.ToInt32(arrayFirstToDate[2]);
            DateTime firstToDateValue = new DateTime(firstToYear, firstToMonth, firstToDay);
            ViewBag.FirstToDate = firstToDateValue;


            //wrong datetime inputs => return view old view
            bool isDateFalse = false;
            if (fromDateValue > toDateValue)
            {
                isDateFalse = true;
                fromDateValue = firstFromDateValue;
                toDateValue = firstToDateValue;
                ViewBag.Alert = isDateFalse;
            }
            ViewBag.Alert = isDateFalse;

            //DateTime fDate = DateTime.Parse(fromDateValue);
            //DateTime tDate = DateTime.Parse(toDateValue);
            List<ReportTube> preData = _reportBLO.GetATheaterData(theaterId, fromDateValue, toDateValue).OrderBy(theater => theater.ReportDate).ToList();
            List<ReportTube> data = SortSystemDataInMonth(preData);
            //calculate total resource of data
            List<ReportTube> tempData = _reportBLO.GetATheaterData(theaterId, fromDateValue, toDateValue).OrderBy(theater => theater.TheaterName).ToList();
            double totalBalance = 0;
            double totalRevenue = 0;
            int totalNumBalance = 0;
            foreach (var item in tempData)
            {
                //calculating balance in each tube
                totalBalance = totalBalance + item.Balance;
                totalRevenue = totalRevenue + item.TotalPrice;
                totalNumBalance = totalNumBalance + item.NumberOfSoldTicket;
            }
            ViewBag.TotalBalance = totalBalance;
            ViewBag.TotalRevenue = totalRevenue;
            ViewBag.TotalNumBalance = totalNumBalance;
            //===================Paging +sorting Model===================
            ViewBag.NameSortParm = "date_desc";
            ViewBag.CurrentSort = sortOrder;

            if (searchString != null)
            {
                page = 1;
            }
            else
            {
                searchString = currentFilter;
            }

            ViewBag.CurrentFilter = searchString;
            ViewBag.IsSortDes = false;

            if (!String.IsNullOrEmpty(searchString))
            {
                data = data.Where(theater => theater.TheaterName.ToUpper().Contains(searchString.ToUpper())
                    || theater.ReportDate.ToShortDateString().ToUpper().Contains(searchString.ToUpper())
                                                  ).ToList();
            }
            switch (sortOrder)
            {
                case "date_desc":
                    if (isSortDescending == true)
                    {
                        data = data.OrderByDescending(theater => theater.ReportDate).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        data = data.OrderBy(theater => theater.ReportDate).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                case "sold_ticket_desc":
                    if (isSortDescending == true)
                    {
                        data = data.OrderByDescending(theater => theater.NumberOfSoldTicket).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        data = data.OrderBy(theater => theater.NumberOfSoldTicket).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                case "revenue_desc":
                    if (isSortDescending == true)
                    {
                        data = data.OrderByDescending(theater => theater.TotalPrice).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        data = data.OrderBy(theater => theater.TotalPrice).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                case "balance_desc":
                    if (isSortDescending == true)
                    {
                        data = data.OrderByDescending(theater => (theater.TotalPrice - theater.TotalPurePrice)).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        data = data.OrderBy(theater => (theater.TotalPrice - theater.TotalPurePrice)).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                default:
                    data = data.OrderBy(theater => theater.ReportDate).ToList();
                    ViewBag.IsSortDes = false;
                    break;
            }

            int pageSize = 9;
            int pageNumber = (page ?? 1);
            ViewBag.Theater = theaterId;
            ViewBag.FDate = fromDateValue;
            ViewBag.TDate = toDateValue;
            ViewBag.TheaterId = theaterId;
            ViewBag.Supplier = usn;
            ViewBag.DataSize = tempData.Count;
            return View(data.ToPagedList(pageNumber, pageSize));
        }
        public ActionResult SystemTheaterYearsDetail(string usn, int theaterId, string fTime, string tTime, string sortOrder, string currentFilter, string searchString, int? page, bool? isSortDescending, string firstFromTime, string firstToTime)
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Admin")
            {
                return Redirect("/Home/Announcement");
            }
            // Specify exactly how to interpret the string.
            IFormatProvider culture = new System.Globalization.CultureInfo("en-US", true);

            var arrayFromDate = fTime.Split('/');
            int fromDay = Convert.ToInt32(arrayFromDate[0]);
            int fromMonth = Convert.ToInt32(arrayFromDate[1]);
            int fromYear = Convert.ToInt32(arrayFromDate[2]);
            DateTime fromDateValue = new DateTime(fromYear, fromMonth, fromDay);

            var arrayToDate = tTime.Split('/');
            int toDay = Convert.ToInt32(arrayToDate[0]);
            int toMonth = Convert.ToInt32(arrayToDate[1]);
            int toYear = Convert.ToInt32(arrayToDate[2]);
            DateTime toDateValue = new DateTime(toYear, toMonth, toDay);

            var arrayFirstFromDate = firstFromTime.Split('/');
            int firstFromDay = Convert.ToInt32(arrayFirstFromDate[0]);
            int firstfromMonth = Convert.ToInt32(arrayFirstFromDate[1]);
            int firstFromYear = Convert.ToInt32(arrayFirstFromDate[2]);
            DateTime firstFromDateValue = new DateTime(firstFromYear, firstfromMonth, firstFromDay);
            ViewBag.FirstFromDate = firstFromDateValue;

            var arrayFirstToDate = firstToTime.Split('/');
            int firstToDay = Convert.ToInt32(arrayFirstToDate[0]);
            int firstToMonth = Convert.ToInt32(arrayFirstToDate[1]);
            int firstToYear = Convert.ToInt32(arrayFirstToDate[2]);
            DateTime firstToDateValue = new DateTime(firstToYear, firstToMonth, firstToDay);
            ViewBag.FirstToDate = firstToDateValue;   

            //wrong datetime inputs => return view old view
            bool isDateFalse = false;
            if (fromDateValue > toDateValue)
            {
                isDateFalse = true;
                fromDateValue = firstFromDateValue;
                toDateValue = firstToDateValue;
                ViewBag.Alert = isDateFalse;
            }
            ViewBag.Alert = isDateFalse;

            //DateTime fDate = DateTime.Parse(fromDateValue);
            //DateTime tDate = DateTime.Parse(toDateValue);
            List<ReportTube> preData = _reportBLO.GetATheaterData(theaterId, fromDateValue, toDateValue).OrderBy(theater => theater.ReportDate).ToList();
            List<ReportTube> data = SortSystemDataInYear(preData);
            //calculate total resource of data
            List<ReportTube> tempData = _reportBLO.GetATheaterData(theaterId, fromDateValue, toDateValue).OrderBy(theater => theater.TheaterName).ToList();
            double totalBalance = 0;
            double totalRevenue = 0;
            int totalNumBalance = 0;
            foreach (var item in tempData)
            {
                //calculating balance in each tube
                totalBalance = totalBalance + item.Balance;
                totalRevenue = totalRevenue + item.TotalPrice;
                totalNumBalance = totalNumBalance + item.NumberOfSoldTicket;
            }
            ViewBag.TotalBalance = totalBalance;
            ViewBag.TotalRevenue = totalRevenue;
            ViewBag.TotalNumBalance = totalNumBalance;
            //===================Paging +sorting Model===================
            ViewBag.NameSortParm = "date_desc";
            ViewBag.CurrentSort = sortOrder;

            if (searchString != null)
            {
                page = 1;
            }
            else
            {
                searchString = currentFilter;
            }

            ViewBag.CurrentFilter = searchString;
            ViewBag.IsSortDes = false;

            if (!String.IsNullOrEmpty(searchString))
            {
                data = data.Where(theater => theater.TheaterName.ToUpper().Contains(searchString.ToUpper())
                    || theater.ReportDate.ToShortDateString().ToUpper().Contains(searchString.ToUpper())
                                                  ).ToList();
            }
            switch (sortOrder)
            {
                case "date_desc":
                    if (isSortDescending == true)
                    {
                        data = data.OrderByDescending(theater => theater.ReportDate).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        data = data.OrderBy(theater => theater.ReportDate).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                case "sold_ticket_desc":
                    if (isSortDescending == true)
                    {
                        data = data.OrderByDescending(theater => theater.NumberOfSoldTicket).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        data = data.OrderBy(theater => theater.NumberOfSoldTicket).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                case "revenue_desc":
                    if (isSortDescending == true)
                    {
                        data = data.OrderByDescending(theater => theater.TotalPrice).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        data = data.OrderBy(theater => theater.TotalPrice).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                case "balance_desc":
                    if (isSortDescending == true)
                    {
                        data = data.OrderByDescending(theater => (theater.TotalPrice - theater.TotalPurePrice)).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        data = data.OrderBy(theater => (theater.TotalPrice - theater.TotalPurePrice)).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                default:
                    data = data.OrderBy(theater => theater.ReportDate).ToList();
                    ViewBag.IsSortDes = false;
                    break;
            }

            int pageSize = 9;
            int pageNumber = (page ?? 1);
            ViewBag.Theater = theaterId;
            ViewBag.FDate = fromDateValue;
            ViewBag.TDate = toDateValue;
            ViewBag.TheaterId = theaterId;
            ViewBag.Supplier = usn;
            ViewBag.DataSize = tempData.Count;
            return View(data.ToPagedList(pageNumber, pageSize));
        }

        //Supplier view 
        public ActionResult SupplierTheaterDaysDetail(string usn, int theaterId, string fTime, string tTime, string sortOrder, string currentFilter, string searchString, int? page, bool? isSortDescending, string firstFromTime, string firstToTime)
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Supplier")
            {
                return Redirect("/Home/Announcement");
            }
            // Specify exactly how to interpret the string.
            IFormatProvider culture = new System.Globalization.CultureInfo("en-US", true);

            var arrayFromDate = fTime.Split('/');
            int fromDay = Convert.ToInt32(arrayFromDate[0]);
            int fromMonth = Convert.ToInt32(arrayFromDate[1]);
            int fromYear = Convert.ToInt32(arrayFromDate[2]);
            DateTime fromDateValue = new DateTime(fromYear, fromMonth, fromDay);

            var arrayToDate = tTime.Split('/');
            int toDay = Convert.ToInt32(arrayToDate[0]);
            int toMonth = Convert.ToInt32(arrayToDate[1]);
            int toYear = Convert.ToInt32(arrayToDate[2]);
            DateTime toDateValue = new DateTime(toYear, toMonth, toDay);

            var arrayFirstFromDate = firstFromTime.Split('/');
            int firstFromDay = Convert.ToInt32(arrayFirstFromDate[0]);
            int firstfromMonth = Convert.ToInt32(arrayFirstFromDate[1]);
            int firstFromYear = Convert.ToInt32(arrayFirstFromDate[2]);
            DateTime firstFromDateValue = new DateTime(firstFromYear, firstfromMonth, firstFromDay);
            ViewBag.FirstFromDate = firstFromDateValue;

            var arrayFirstToDate = firstToTime.Split('/');
            int firstToDay = Convert.ToInt32(arrayFirstToDate[0]);
            int firstToMonth = Convert.ToInt32(arrayFirstToDate[1]);
            int firstToYear = Convert.ToInt32(arrayFirstToDate[2]);
            DateTime firstToDateValue = new DateTime(firstToYear, firstToMonth, firstToDay);
            ViewBag.FirstToDate = firstToDateValue;

            //wrong datetime inputs => return view old view
            bool isDateFalse = false;
            if (fromDateValue > toDateValue)
            {
                isDateFalse = true;
                fromDateValue = firstFromDateValue;
                toDateValue = firstToDateValue;
                ViewBag.Alert = isDateFalse;
            }
            ViewBag.Alert = isDateFalse;

            //DateTime fDate = DateTime.Parse(fromDateValue);
            //DateTime tDate = DateTime.Parse(toDateValue);
            List<ReportTube> data = _reportBLO.GetATheaterData(theaterId, fromDateValue, toDateValue).OrderBy(theater => theater.ReportDate).ToList();
            //calculate total resource of data
            List<ReportTube> tempData = _reportBLO.GetATheaterData(theaterId, fromDateValue, toDateValue).OrderBy(theater => theater.TheaterName).ToList();
            double totalBalance = 0;
            double totalRevenue = 0;
            int totalNumBalance = 0;
            foreach (var item in tempData)
            {
                //calculating balance in each tube
                totalBalance = totalBalance + item.Balance;
                totalNumBalance = totalNumBalance + item.NumberOfSoldTicket;
                totalRevenue = totalRevenue + item.TotalPrice;
            }
            ViewBag.TotalBalance = totalBalance;
            ViewBag.TotalRevenue = totalRevenue;
            ViewBag.totalNumBalance = totalNumBalance;
            //===================Paging +sorting Model===================
            ViewBag.NameSortParm = "date_desc";
            ViewBag.CurrentSort = sortOrder;

            if (searchString != null)
            {
                page = 1;
            }
            else
            {
                searchString = currentFilter;
            }

            ViewBag.CurrentFilter = searchString;
            ViewBag.IsSortDes = false;

            if (!String.IsNullOrEmpty(searchString))
            {
                data = data.Where(theater => theater.TheaterName.ToUpper().Contains(searchString.ToUpper())
                    || theater.ReportDate.ToShortDateString().ToUpper().Contains(searchString.ToUpper())
                                                  ).ToList();
            }
            switch (sortOrder)
            {
                case "date_desc":
                    if (isSortDescending == true)
                    {
                        data = data.OrderByDescending(theater => theater.ReportDate).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        data = data.OrderBy(theater => theater.ReportDate).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                case "sold_ticket_desc":
                    if (isSortDescending == true)
                    {
                        data = data.OrderByDescending(theater => theater.NumberOfSoldTicket).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        data = data.OrderBy(theater => theater.NumberOfSoldTicket).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                case "revenue_desc":
                    if (isSortDescending == true)
                    {
                        data = data.OrderByDescending(theater => theater.TotalPrice).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        data = data.OrderBy(theater => theater.TotalPrice).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                default:
                    data = data.OrderBy(theater => theater.ReportDate).ToList();
                    ViewBag.IsSortDes = false;
                    break;
            }

            int pageSize = 9;
            int pageNumber = (page ?? 1);
            ViewBag.Theater = theaterId;
            ViewBag.FDate = fromDateValue;
            ViewBag.TDate = toDateValue;
            ViewBag.TheaterId = theaterId;
            ViewBag.Supplier = usn;
            ViewBag.DataSize = tempData.Count;
            return View(data.ToPagedList(pageNumber, pageSize));
        }
        public ActionResult SupplierTheaterMonthsDetail(string usn, int theaterId, string fTime, string tTime, string sortOrder, string currentFilter, string searchString, int? page, bool? isSortDescending, string firstFromTime, string firstToTime)
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Supplier")
            {
                return Redirect("/Home/Announcement");
            }
            // Specify exactly how to interpret the string.
            IFormatProvider culture = new System.Globalization.CultureInfo("en-US", true);

            var arrayFromDate = fTime.Split('/');
            int fromDay = Convert.ToInt32(arrayFromDate[0]);
            int fromMonth = Convert.ToInt32(arrayFromDate[1]);
            int fromYear = Convert.ToInt32(arrayFromDate[2]);
            DateTime fromDateValue = new DateTime(fromYear, fromMonth, fromDay);

            var arrayToDate = tTime.Split('/');
            int toDay = Convert.ToInt32(arrayToDate[0]);
            int toMonth = Convert.ToInt32(arrayToDate[1]);
            int toYear = Convert.ToInt32(arrayToDate[2]);
            DateTime toDateValue = new DateTime(toYear, toMonth, toDay);

            var arrayFirstFromDate = firstFromTime.Split('/');
            int firstFromDay = Convert.ToInt32(arrayFirstFromDate[0]);
            int firstfromMonth = Convert.ToInt32(arrayFirstFromDate[1]);
            int firstFromYear = Convert.ToInt32(arrayFirstFromDate[2]);
            DateTime firstFromDateValue = new DateTime(firstFromYear, firstfromMonth, firstFromDay);
            ViewBag.FirstFromDate = firstFromDateValue;

            var arrayFirstToDate = firstToTime.Split('/');
            int firstToDay = Convert.ToInt32(arrayFirstToDate[0]);
            int firstToMonth = Convert.ToInt32(arrayFirstToDate[1]);
            int firstToYear = Convert.ToInt32(arrayFirstToDate[2]);
            DateTime firstToDateValue = new DateTime(firstToYear, firstToMonth, firstToDay);
            ViewBag.FirstToDate = firstToDateValue;

            //wrong datetime inputs => return view old view
            bool isDateFalse = false;
            if (fromDateValue > toDateValue)
            {
                isDateFalse = true;
                fromDateValue = firstFromDateValue;
                toDateValue = firstToDateValue;
                ViewBag.Alert = isDateFalse;
            }
            ViewBag.Alert = isDateFalse;
                                                    
            //DateTime fDate = DateTime.Parse(fromDateValue);
            //DateTime tDate = DateTime.Parse(toDateValue);
            List<ReportTube> preData = _reportBLO.GetATheaterData(theaterId, fromDateValue, toDateValue).OrderBy(theater => theater.ReportDate).ToList();
            List<ReportTube> data = SortSystemDataInMonth(preData);
            //calculate total resource of data
            List<ReportTube> tempData = _reportBLO.GetATheaterData(theaterId, fromDateValue, toDateValue).OrderBy(theater => theater.TheaterName).ToList();
            double totalBalance = 0;
            double totalRevenue = 0;
            int totalNumBalance = 0;
            foreach (var item in tempData)
            {
                //calculating balance in each tube
                totalBalance = totalBalance + item.Balance;
                totalNumBalance = totalNumBalance + item.NumberOfSoldTicket;
                totalRevenue = totalRevenue + item.TotalPrice;
            }
            ViewBag.TotalBalance = totalBalance;
            ViewBag.TotalRevenue = totalRevenue;
            ViewBag.totalNumBalance = totalNumBalance;
            //===================Paging +sorting Model===================
            ViewBag.NameSortParm = "date_desc";
            ViewBag.CurrentSort = sortOrder;

            if (searchString != null)
            {
                page = 1;
            }
            else
            {
                searchString = currentFilter;
            }

            ViewBag.CurrentFilter = searchString;
            ViewBag.IsSortDes = false;

            if (!String.IsNullOrEmpty(searchString))
            {
                data = data.Where(theater => theater.TheaterName.ToUpper().Contains(searchString.ToUpper())
                    || theater.ReportDate.ToShortDateString().ToUpper().Contains(searchString.ToUpper())
                                                  ).ToList();
            }
            switch (sortOrder)
            {
                case "date_desc":
                    if (isSortDescending == true)
                    {
                        data = data.OrderByDescending(theater => theater.ReportDate.Month).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        data = data.OrderBy(theater => theater.ReportDate.Month).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                case "sold_ticket_desc":
                    if (isSortDescending == true)
                    {
                        data = data.OrderByDescending(theater => theater.NumberOfSoldTicket).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        data = data.OrderBy(theater => theater.NumberOfSoldTicket).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                case "revenue_desc":
                    if (isSortDescending == true)
                    {
                        data = data.OrderByDescending(theater => theater.TotalPrice).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        data = data.OrderBy(theater => theater.TotalPrice).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                default:
                    data = data.OrderBy(theater => theater.ReportDate.Month).ToList();
                    ViewBag.IsSortDes = false;
                    break;
            }

            int pageSize = 9;
            int pageNumber = (page ?? 1);
            ViewBag.Theater = theaterId;
            ViewBag.FDate = fromDateValue;
            ViewBag.TDate = toDateValue;
            ViewBag.TheaterId = theaterId;
            ViewBag.Supplier = usn;
            ViewBag.DataSize = tempData.Count;
            return View(data.ToPagedList(pageNumber, pageSize));
        }
        public ActionResult SupplierTheaterYearsDetail(string usn, int theaterId, string fTime, string tTime, string sortOrder, string currentFilter, string searchString, int? page, bool? isSortDescending, string firstFromTime, string firstToTime)
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Supplier")
            {
                return Redirect("/Home/Announcement");
            }
            // Specify exactly how to interpret the string.
            IFormatProvider culture = new System.Globalization.CultureInfo("en-US", true);

            var arrayFromDate = fTime.Split('/');
            int fromDay = Convert.ToInt32(arrayFromDate[0]);
            int fromMonth = Convert.ToInt32(arrayFromDate[1]);
            int fromYear = Convert.ToInt32(arrayFromDate[2]);
            DateTime fromDateValue = new DateTime(fromYear, fromMonth, fromDay);

            var arrayToDate = tTime.Split('/');
            int toDay = Convert.ToInt32(arrayToDate[0]);
            int toMonth = Convert.ToInt32(arrayToDate[1]);
            int toYear = Convert.ToInt32(arrayToDate[2]);
            DateTime toDateValue = new DateTime(toYear, toMonth, toDay);

            var arrayFirstFromDate = firstFromTime.Split('/');
            int firstFromDay = Convert.ToInt32(arrayFirstFromDate[0]);
            int firstfromMonth = Convert.ToInt32(arrayFirstFromDate[1]);
            int firstFromYear = Convert.ToInt32(arrayFirstFromDate[2]);
            DateTime firstFromDateValue = new DateTime(firstFromYear, firstfromMonth, firstFromDay);
            ViewBag.FirstFromDate = firstFromDateValue;

            var arrayFirstToDate = firstToTime.Split('/');
            int firstToDay = Convert.ToInt32(arrayFirstToDate[0]);
            int firstToMonth = Convert.ToInt32(arrayFirstToDate[1]);
            int firstToYear = Convert.ToInt32(arrayFirstToDate[2]);
            DateTime firstToDateValue = new DateTime(firstToYear, firstToMonth, firstToDay);
            ViewBag.FirstToDate = firstToDateValue;  

            //wrong datetime inputs => return view old view
            bool isDateFalse = false;
            if (fromDateValue > toDateValue)
            {
                isDateFalse = true;
                fromDateValue = firstFromDateValue;
                toDateValue = firstToDateValue;
                ViewBag.Alert = isDateFalse;
            }
            ViewBag.Alert = isDateFalse;

            //DateTime fDate = DateTime.Parse(fromDateValue);
            //DateTime tDate = DateTime.Parse(toDateValue);
            List<ReportTube> preData = _reportBLO.GetATheaterData(theaterId, fromDateValue, toDateValue).OrderBy(theater => theater.ReportDate).ToList();
            List<ReportTube> data = SortSystemDataInYear(preData);
            //calculate total resource of data
            List<ReportTube> tempData = _reportBLO.GetATheaterData(theaterId, fromDateValue, toDateValue).OrderBy(theater => theater.TheaterName).ToList();
            double totalBalance = 0;
            double totalRevenue = 0;
            int totalNumBalance = 0;
            foreach (var item in tempData)
            {
                //calculating balance in each tube
                totalBalance = totalBalance + item.Balance;
                totalNumBalance = totalNumBalance + item.NumberOfSoldTicket;
                totalRevenue = totalRevenue + item.TotalPrice;
            }
            ViewBag.TotalBalance = totalBalance;
            ViewBag.TotalRevenue = totalRevenue;
            ViewBag.totalNumBalance = totalNumBalance;
            //===================Paging +sorting Model===================
            ViewBag.NameSortParm = "date_desc";
            ViewBag.CurrentSort = sortOrder;

            if (searchString != null)
            {
                page = 1;
            }
            else
            {
                searchString = currentFilter;
            }

            ViewBag.CurrentFilter = searchString;
            ViewBag.IsSortDes = false;

            if (!String.IsNullOrEmpty(searchString))
            {
                data = data.Where(theater => theater.TheaterName.ToUpper().Contains(searchString.ToUpper())
                    || theater.ReportDate.ToShortDateString().ToUpper().Contains(searchString.ToUpper())
                                                  ).ToList();
            }
            switch (sortOrder)
            {
                case "date_desc":
                    if (isSortDescending == true)
                    {
                        data = data.OrderByDescending(theater => theater.ReportDate.Year).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        data = data.OrderBy(theater => theater.ReportDate.Year).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                case "sold_ticket_desc":
                    if (isSortDescending == true)
                    {
                        data = data.OrderByDescending(theater => theater.NumberOfSoldTicket).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        data = data.OrderBy(theater => theater.NumberOfSoldTicket).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                case "revenue_desc":
                    if (isSortDescending == true)
                    {
                        data = data.OrderByDescending(theater => theater.TotalPrice).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        data = data.OrderBy(theater => theater.TotalPrice).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                default:
                    data = data.OrderBy(theater => theater.ReportDate.Year).ToList();
                    ViewBag.IsSortDes = false;
                    break;
            }

            int pageSize = 9;
            int pageNumber = (page ?? 1);
            ViewBag.Theater = theaterId;
            ViewBag.FDate = fromDateValue;
            ViewBag.TDate = toDateValue;
            ViewBag.TheaterId = theaterId;
            ViewBag.Supplier = usn;
            ViewBag.DataSize = tempData.Count;
            return View(data.ToPagedList(pageNumber, pageSize));
        }

        #endregion

        #region  Group data in supplier name or theater name - just use for sold data
        public List<ReportTube> GroupDataInSupplier(List<ReportTube> data)
        {
            if (data.Count == 0)
            {
                return data;
            }
            List<ReportTube> result = new List<ReportTube>();
            var listRecords = (from d in data orderby d.SupplierId ascending select d).ToList();
            string supplier = null;
            bool add = false;
            ReportTube tube = new ReportTube();
            foreach (var item in listRecords)
            {
                if (item.SupplierId != supplier)
                {
                    if (add == true)
                    {
                        result.Add(tube);
                        tube = new ReportTube();
                    }
                    supplier = item.SupplierId;
                    //create new tube
                    tube.SupplierId = item.SupplierId;
                    tube.SupplierName = item.SupplierName;
                    tube.TheaterId = item.TheaterId;
                    tube.TheaterName = item.TheaterName;
                    tube.CategoryName = item.CategoryName;
                    tube.TotalPrice = item.TotalPrice;
                    tube.TotalPurePrice = item.TotalPurePrice;
                    tube.NumberOfSoldTicket = item.NumberOfSoldTicket;
                    tube.NumberOfTicket = item.NumberOfTicket;
                    tube.ReportDate = item.ReportDate;
                    tube.ReportDay = item.ReportDay;
                    if (add == false) { add = true; }
                }
                else
                {
                    tube.TotalPrice = tube.TotalPrice + item.TotalPrice;
                    tube.TotalPurePrice = tube.TotalPurePrice + item.TotalPurePrice;
                    tube.NumberOfSoldTicket = tube.NumberOfSoldTicket + item.NumberOfSoldTicket;
                    tube.NumberOfTicket = tube.NumberOfTicket + item.NumberOfTicket;
                    if (add == false) { add = true; }
                }
            }
            result.Add(tube);
            return result;
        }
        public List<ReportTube> GroupDataInTheater(List<ReportTube> data)
        {
            if (data.Count == 0)
            {
                return data;
            }
            List<ReportTube> result = new List<ReportTube>();
            var listRecords = (from d in data orderby d.TheaterId ascending select d).ToList();
            int theater = 0;
            bool add = false;
            ReportTube tube = new ReportTube();
            foreach (var item in listRecords)
            {
                if (item.TheaterId != theater)
                {
                    if (add == true)
                    {
                        result.Add(tube);
                        tube = new ReportTube();
                    }
                    theater = item.TheaterId;
                    //create new tube
                    tube.SupplierId = item.SupplierId;
                    tube.SupplierName = item.SupplierName;
                    tube.TheaterId = item.TheaterId;
                    tube.TheaterName = item.TheaterName;
                    tube.CategoryName = item.CategoryName;
                    tube.TotalPrice = item.TotalPrice;
                    tube.TotalPurePrice = item.TotalPurePrice;
                    tube.NumberOfSoldTicket = item.NumberOfSoldTicket;
                    tube.NumberOfTicket = item.NumberOfTicket;
                    tube.ReportDate = item.ReportDate;
                    tube.ReportDay = item.ReportDay;
                    if (add == false) { add = true; }
                }
                else
                {
                    tube.TotalPrice = tube.TotalPrice + item.TotalPrice;
                    tube.TotalPurePrice = tube.TotalPurePrice + item.TotalPurePrice;
                    tube.NumberOfSoldTicket = tube.NumberOfSoldTicket + item.NumberOfSoldTicket;
                    tube.NumberOfTicket = tube.NumberOfTicket + item.NumberOfTicket;
                    if (add == false) { add = true; }
                }
            }
            result.Add(tube);
            return result;
        }
        #endregion

        #region Group Data in groups of months or groups of years - get best sale days and best sale months
        /// <summary>
        ///  Group data in tubes of years
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public List<ReportTube> SortSystemDataInYear(List<ReportTube> data)
        {
            if (data.Count == 0)
            {
                return data;
            }
            List<ReportTube> sortedData = new List<ReportTube>();
            string supplier = null;
            int year = 0;
            bool add = false;
            ReportTube reportTube = new ReportTube();
            List<ReportTube> preData = (from d in data orderby d.SupplierId, d.ReportDate.Year select d).ToList();
            foreach (var item in preData)
            {
                if (!(supplier == item.SupplierId && year == item.ReportDate.Year))
                {
                    if (add == true)
                    {
                        // before create new tube, push the old tube to reportData
                        sortedData.Add(reportTube);
                        reportTube = new ReportTube();
                    }
                    supplier = item.SupplierId;
                    year = item.ReportDate.Year;
                    // create new tube                          
                    reportTube.TheaterId = item.TheaterId;
                    reportTube.TheaterName = item.TheaterName;
                    reportTube.ReportDate = item.ReportDate;
                    reportTube.SupplierName = item.SupplierName;
                    reportTube.SupplierId = item.SupplierId;
                    reportTube.NumberOfTicket = item.NumberOfTicket;
                    reportTube.TotalPurePrice = item.TotalPurePrice;
                    if (item.NumberOfSoldTicket > 0)
                    {
                        reportTube.NumberOfSoldTicket = item.NumberOfSoldTicket;
                        reportTube.TotalPrice = item.TotalPrice;
                        reportTube.Balance = item.Balance;
                    }
                    if (item.NumberOfReturnTicket > 0)
                    {
                        reportTube.NumberOfReturnTicket = item.NumberOfReturnTicket;
                    }
                    if (add == false) { add = true; }
                }
                // else, update old one
                else
                {
                    //update number of tiket and totalprice
                    reportTube.NumberOfTicket = reportTube.NumberOfTicket + item.NumberOfTicket;
                    reportTube.TotalPurePrice = reportTube.TotalPurePrice + item.TotalPurePrice;
                    if (item.NumberOfSoldTicket > 0)
                    {
                        reportTube.NumberOfSoldTicket = reportTube.NumberOfSoldTicket + item.NumberOfSoldTicket;
                        reportTube.TotalPrice = reportTube.TotalPrice + item.TotalPrice;
                        reportTube.Balance = reportTube.Balance + item.Balance;
                    }
                    if (item.NumberOfReturnTicket > 0)
                    {
                        reportTube.NumberOfReturnTicket = reportTube.NumberOfReturnTicket + item.NumberOfReturnTicket;
                    }
                    if (add == false) { add = true; }
                }
            }
            sortedData.Add(reportTube);
            List<ReportTube> fData = (from d in sortedData orderby d.ReportDate.Year, d.TheaterName select d).ToList();
            return fData;
        }
        /// <summary>
        ///  group data in tubes of months
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public List<ReportTube> SortSystemDataInMonth(List<ReportTube> data)
        {
            if (data.Count == 0)
            {
                return data;
            }
            List<ReportTube> sortedData = new List<ReportTube>();
            string supplier = null;
            int month = 0;
            int year = 0;
            bool add = false;
            ReportTube reportTube = new ReportTube();
            List<ReportTube> preData = (from d in data orderby d.SupplierId, d.ReportDate.Year, d.ReportDate.Month ascending select d).ToList();
            foreach (var item in preData)
            {
                if (!(supplier == item.SupplierId && month == item.ReportDate.Month && year == item.ReportDate.Year))
                {
                    if (add == true)
                    {
                        // before create new tube, push the old tube to reportData
                        sortedData.Add(reportTube);
                        reportTube = new ReportTube();
                    }
                    supplier = item.SupplierId;
                    month = item.ReportDate.Month;
                    year = item.ReportDate.Year;
                    // create new tube                     
                    reportTube.TheaterId = item.TheaterId;
                    reportTube.TheaterName = item.TheaterName;
                    reportTube.ReportDate = item.ReportDate;
                    reportTube.SupplierName = item.SupplierName;
                    reportTube.SupplierId = item.SupplierId;
                    reportTube.NumberOfTicket = item.NumberOfTicket;
                    reportTube.TotalPurePrice = item.TotalPurePrice;
                    if (item.NumberOfSoldTicket > 0)
                    {
                        reportTube.NumberOfSoldTicket = item.NumberOfSoldTicket;
                        reportTube.TotalPrice = item.TotalPrice;
                        reportTube.Balance = item.Balance;
                    }
                    if (item.NumberOfReturnTicket > 0)
                    {
                        reportTube.NumberOfReturnTicket = item.NumberOfReturnTicket;
                    }
                    if (add == false) { add = true; }
                }
                // else, update old one
                else
                {
                    //update number of tiket and totalprice
                    reportTube.NumberOfTicket = reportTube.NumberOfTicket + item.NumberOfTicket;
                    reportTube.TotalPurePrice = reportTube.TotalPurePrice + item.TotalPurePrice;
                    if (item.NumberOfSoldTicket > 0)
                    {
                        reportTube.NumberOfSoldTicket = reportTube.NumberOfSoldTicket + item.NumberOfSoldTicket;
                        reportTube.TotalPrice = reportTube.TotalPrice + item.TotalPrice;
                        reportTube.Balance = reportTube.Balance + item.Balance;
                    }
                    if (item.NumberOfReturnTicket > 0)
                    {
                        reportTube.NumberOfReturnTicket = reportTube.NumberOfReturnTicket + item.NumberOfReturnTicket;
                    }
                    if (add == false) { add = true; }
                }
            }
            sortedData.Add(reportTube);
            List<ReportTube> fData = (from d in sortedData orderby d.ReportDate.Year, d.ReportDate.Month, d.TheaterName ascending select d).ToList();
            return fData;
        }

        /// <summary>
        ///  group theaters data in tubes of years
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public List<ReportTube> SortSystemTheatersDataInYear(List<ReportTube> data)
        {
            if (data.Count == 0)
            {
                return data;
            }
            List<ReportTube> sortedData = new List<ReportTube>();
            int theater = 0;
            int year = 0;
            bool add = false;
            ReportTube reportTube = new ReportTube();
            List<ReportTube> preData = (from d in data orderby d.TheaterName, d.ReportDate.Year select d).ToList();
            foreach (var item in preData)
            {
                if (!(theater == item.TheaterId && year == item.ReportDate.Year))
                {
                    if (add == true)
                    {
                        // before create new tube, push the old tube to reportData
                        sortedData.Add(reportTube);
                        reportTube = new ReportTube();
                    }
                    theater = item.TheaterId;
                    year = item.ReportDate.Year;
                    // create new tube
                    reportTube.TheaterId = item.TheaterId;
                    reportTube.TheaterName = item.TheaterName;
                    reportTube.ReportDate = item.ReportDate;
                    reportTube.SupplierName = item.SupplierName;
                    reportTube.SupplierId = item.SupplierId;
                    reportTube.NumberOfTicket = item.NumberOfTicket;
                    reportTube.TotalPurePrice = item.TotalPurePrice;
                    if (item.NumberOfSoldTicket > 0)
                    {
                        reportTube.NumberOfSoldTicket = item.NumberOfSoldTicket;
                        reportTube.TotalPrice = item.TotalPrice;
                        reportTube.Balance = item.Balance;
                    }
                    if (item.NumberOfReturnTicket > 0)
                    {
                        reportTube.NumberOfReturnTicket = item.NumberOfReturnTicket;
                    }
                    if (add == false) { add = true; }
                }
                // else, update old one
                else
                {
                    //update number of tiket and totalprice
                    reportTube.NumberOfTicket = reportTube.NumberOfTicket + item.NumberOfTicket;
                    reportTube.TotalPurePrice = reportTube.TotalPurePrice + item.TotalPurePrice;
                    if (item.NumberOfSoldTicket > 0)
                    {
                        reportTube.NumberOfSoldTicket = reportTube.NumberOfSoldTicket + item.NumberOfSoldTicket;
                        reportTube.TotalPrice = reportTube.TotalPrice + item.TotalPrice;
                        reportTube.Balance = reportTube.Balance + item.Balance;
                    }
                    if (item.NumberOfReturnTicket > 0)
                    {
                        reportTube.NumberOfReturnTicket = reportTube.NumberOfReturnTicket + item.NumberOfReturnTicket;
                    }
                    if (add == false) { add = true; }
                }
            }
            if (reportTube.TotalPurePrice != 0)
            {
                sortedData.Add(reportTube);
            }
            List<ReportTube> fData = (from d in sortedData orderby d.ReportDate.Year, d.TheaterName select d).ToList();
            return fData;
        }
        /// <summary>
        /// group theaters data in tubes of months
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public List<ReportTube> SortSystemTheatersDataInMonth(List<ReportTube> data)
        {
            if (data.Count == 0)
            {
                return data;
            }
            List<ReportTube> sortedData = new List<ReportTube>();
            int theater = 0;
            int month = 0;
            bool add = false;
            ReportTube reportTube = new ReportTube();
            List<ReportTube> preData = (from d in data orderby d.TheaterName, d.ReportDate.Year, d.ReportDate.Month ascending select d).ToList();
            foreach (var item in preData)
            {
                if (!(theater == item.TheaterId && month == item.ReportDate.Month))
                {
                    if (add == true)
                    {
                        // before create new tube, push the old tube to reportData
                        sortedData.Add(reportTube);
                        reportTube = new ReportTube();
                    }
                    theater = item.TheaterId;
                    month = item.ReportDate.Month;
                    // create new tube
                    reportTube.TheaterId = item.TheaterId;
                    reportTube.TheaterName = item.TheaterName;
                    reportTube.ReportDate = item.ReportDate;
                    reportTube.SupplierId = item.SupplierId;
                    reportTube.SupplierName = item.SupplierName;
                    reportTube.NumberOfTicket = item.NumberOfTicket;
                    reportTube.TotalPurePrice = item.TotalPurePrice;
                    if (item.NumberOfSoldTicket > 0)
                    {
                        reportTube.NumberOfSoldTicket = item.NumberOfSoldTicket;
                        reportTube.TotalPrice = item.TotalPrice;
                        reportTube.Balance = item.Balance;
                    }
                    if (item.NumberOfReturnTicket > 0)
                    {
                        reportTube.NumberOfReturnTicket = item.NumberOfReturnTicket;
                    }
                    if (add == false) { add = true; }
                }
                // else, update old one
                else
                {
                    //update number of tiket and totalprice
                    reportTube.NumberOfTicket = reportTube.NumberOfTicket + item.NumberOfTicket;
                    reportTube.TotalPurePrice = reportTube.TotalPurePrice + item.TotalPurePrice;
                    if (item.NumberOfSoldTicket > 0)
                    {
                        reportTube.NumberOfSoldTicket = reportTube.NumberOfSoldTicket + item.NumberOfSoldTicket;
                        reportTube.TotalPrice = reportTube.TotalPrice + item.TotalPrice;
                        reportTube.Balance = reportTube.Balance + item.Balance;
                    }
                    if (item.NumberOfReturnTicket > 0)
                    {
                        reportTube.NumberOfReturnTicket = reportTube.NumberOfReturnTicket + item.NumberOfReturnTicket;
                    }
                    if (add == false) { add = true; }
                }
            }
            if (reportTube.TotalPurePrice != 0)
            {
                sortedData.Add(reportTube);
            }
            List<ReportTube> fData = (from d in sortedData orderby d.ReportDate.Year, d.ReportDate.Month, d.TheaterName ascending select d).ToList();
            return fData;
        }

        /// <summary>
        ///  group categories data in tubes of years
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public List<ReportTube> SortSystemCategoriesDataInYear(List<ReportTube> data)
        {
            if (data.Count == 0)
            {
                return data;
            }
            List<ReportTube> sortedData = new List<ReportTube>();
            string cate = null;
            int year = 0;
            bool add = false;
            ReportTube reportTube = new ReportTube();
            List<ReportTube> preData = (from d in data orderby d.CategoryName, d.ReportDate.Year select d).ToList();
            foreach (var item in preData)
            {
                if (!(cate == item.CategoryName && year == item.ReportDate.Year))
                {
                    if (add == true)
                    {
                        // before create new tube, push the old tube to reportData
                        sortedData.Add(reportTube);
                        reportTube = new ReportTube();
                    }
                    cate = item.CategoryName;
                    year = item.ReportDate.Year;
                    // create new tube                         
                    reportTube.TheaterId = item.TheaterId;
                    reportTube.TheaterName = item.TheaterName;
                    reportTube.CategoryName = item.CategoryName;
                    reportTube.ReportDate = item.ReportDate;
                    reportTube.SupplierName = item.SupplierName;
                    reportTube.SupplierId = item.SupplierId;
                    reportTube.NumberOfTicket = item.NumberOfTicket;
                    reportTube.TotalPurePrice = item.TotalPurePrice;
                    if (item.NumberOfSoldTicket > 0)
                    {
                        reportTube.NumberOfSoldTicket = item.NumberOfSoldTicket;
                        reportTube.TotalPrice = item.TotalPrice;
                        reportTube.Balance = item.Balance;
                    }
                    if (item.NumberOfReturnTicket > 0)
                    {
                        reportTube.NumberOfReturnTicket = item.NumberOfReturnTicket;
                    }
                    if (add == false) { add = true; }
                }
                // else, update old one
                else
                {
                    //update number of tiket and totalprice
                    reportTube.NumberOfTicket = reportTube.NumberOfTicket + item.NumberOfTicket;
                    reportTube.TotalPurePrice = reportTube.TotalPurePrice + item.TotalPurePrice;
                    if (item.NumberOfSoldTicket > 0)
                    {
                        reportTube.NumberOfSoldTicket = reportTube.NumberOfSoldTicket + item.NumberOfSoldTicket;
                        reportTube.TotalPrice = reportTube.TotalPrice + item.TotalPrice;
                        reportTube.Balance = reportTube.Balance + item.Balance;
                    }
                    if (item.NumberOfReturnTicket > 0)
                    {
                        reportTube.NumberOfReturnTicket = reportTube.NumberOfReturnTicket + item.NumberOfReturnTicket;
                    }
                    if (add == false) { add = true; }
                }
            }
            if (reportTube.TotalPurePrice != 0)
            {
                sortedData.Add(reportTube);
            }
            List<ReportTube> fData = (from d in sortedData orderby d.ReportDate.Year, d.CategoryName select d).ToList();
            return fData;
        }
        /// <summary>
        /// group categories data in tubes of months
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public List<ReportTube> SortSystemCategoriesDataInMonth(List<ReportTube> data)
        {
            if (data.Count == 0)
            {
                return data;
            }
            List<ReportTube> sortedData = new List<ReportTube>();
            string cate = null;
            int month = 0;
            bool add = false;
            ReportTube reportTube = new ReportTube();
            List<ReportTube> preData = (from d in data orderby d.CategoryName, d.ReportDate.Month select d).ToList();
            foreach (var item in preData)
            {
                if (!(cate == item.CategoryName && month == item.ReportDate.Month))
                {
                    if (add == true)
                    {
                        // before create new tube, push the old tube to reportData
                        sortedData.Add(reportTube);
                        reportTube = new ReportTube();
                    }
                    cate = item.CategoryName;
                    month = item.ReportDate.Month;
                    // create new tube                            
                    reportTube.TheaterId = item.TheaterId;
                    reportTube.TheaterName = item.TheaterName;
                    reportTube.CategoryName = item.CategoryName;
                    reportTube.ReportDate = item.ReportDate;
                    reportTube.SupplierName = item.SupplierName;
                    reportTube.SupplierId = item.SupplierId;
                    reportTube.NumberOfTicket = item.NumberOfTicket;
                    reportTube.TotalPurePrice = item.TotalPurePrice;
                    if (item.NumberOfSoldTicket > 0)
                    {
                        reportTube.NumberOfSoldTicket = item.NumberOfSoldTicket;
                        reportTube.TotalPrice = item.TotalPrice;
                        reportTube.Balance = item.Balance;
                    }
                    if (item.NumberOfReturnTicket > 0)
                    {
                        reportTube.NumberOfReturnTicket = item.NumberOfReturnTicket;
                    }
                    if (add == false) { add = true; }
                }
                // else, update old one
                else
                {
                    //update number of tiket and totalprice
                    reportTube.NumberOfTicket = reportTube.NumberOfTicket + item.NumberOfTicket;
                    reportTube.TotalPurePrice = reportTube.TotalPurePrice + item.TotalPurePrice;
                    if (item.NumberOfSoldTicket > 0)
                    {
                        reportTube.NumberOfSoldTicket = reportTube.NumberOfSoldTicket + item.NumberOfSoldTicket;
                        reportTube.TotalPrice = reportTube.TotalPrice + item.TotalPrice;
                        reportTube.Balance = reportTube.Balance + item.Balance;
                    }
                    if (item.NumberOfReturnTicket > 0)
                    {
                        reportTube.NumberOfReturnTicket = reportTube.NumberOfReturnTicket + item.NumberOfReturnTicket;
                    }
                    if (add == false) { add = true; }
                }
            }
            if (reportTube.TotalPurePrice != 0)
            {
                sortedData.Add(reportTube);
            }
            List<ReportTube> fData = (from d in sortedData orderby d.ReportDate.Month, d.CategoryName select d).ToList();
            return fData;
        }
        //=============================================================================

        /// <summary>
        ///  group data in tubes of months of many years
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public List<ReportTube> SortSystemDataInMonthOfYear(List<ReportTube> data)
        {
            if (data.Count == 0)
            {
                return data;
            }
            List<ReportTube> sortedData = new List<ReportTube>();
            int month = 0;
            bool add = false;
            ReportTube reportTube = new ReportTube();
            List<ReportTube> preData = (from d in data orderby d.ReportDate.Month select d).ToList();
            foreach (var item in preData)
            {
                if (!(month == item.ReportDate.Month))
                {
                    if (add == true)
                    {
                        // before create new tube, push the old tube to reportData
                        sortedData.Add(reportTube);
                        reportTube = new ReportTube();
                    }
                    month = item.ReportDate.Month;
                    // create new tube
                    reportTube.TheaterId = item.TheaterId;
                    reportTube.TheaterName = item.TheaterName;
                    reportTube.ReportDate = item.ReportDate;
                    reportTube.SupplierName = item.SupplierName;
                    reportTube.NumberOfTicket = item.NumberOfTicket;
                    reportTube.TotalPurePrice = item.TotalPurePrice;
                    if (item.NumberOfSoldTicket > 0)
                    {
                        reportTube.NumberOfSoldTicket = item.NumberOfSoldTicket;
                        reportTube.TotalPrice = item.TotalPrice;
                        reportTube.Balance = item.Balance;
                    }
                    if (add == false) { add = true; }
                }
                // else, update old one
                else
                {
                    //update number of tiket and totalprice
                    reportTube.NumberOfTicket = reportTube.NumberOfTicket + item.NumberOfTicket;
                    reportTube.TotalPurePrice = reportTube.TotalPurePrice + item.TotalPurePrice;
                    if (item.NumberOfSoldTicket > 0)
                    {
                        reportTube.NumberOfSoldTicket = reportTube.NumberOfSoldTicket + item.NumberOfSoldTicket;
                        reportTube.TotalPrice = reportTube.TotalPrice + item.TotalPrice;
                        reportTube.Balance = reportTube.Balance + item.Balance;
                    }
                    if (add == false) { add = true; }
                }
            }
            sortedData.Add(reportTube);
            List<ReportTube> fData = (from d in sortedData orderby d.ReportDate.Month, d.TheaterName select d).ToList();
            return fData;
        }
        /// <summary>
        ///  group data in tubes in days of weeks 
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public List<ReportTube> SortSystemDataInDayOfWeek(List<ReportTube> data)
        {
            if (data.Count == 0)
            {
                return data;
            }
            List<ReportTube> sortedData = new List<ReportTube>();
            int day = -1;
            bool add = false;
            ReportTube reportTube = new ReportTube();
            List<ReportTube> preData = (from d in data orderby d.ReportDate.DayOfWeek select d).ToList();
            foreach (var item in preData)
            {
                if (!(day == (int)item.ReportDate.DayOfWeek))
                {
                    if (add == true)
                    {
                        // before create new tube, push the old tube to reportData
                        sortedData.Add(reportTube);
                        reportTube = new ReportTube();
                    }
                    day = (int)item.ReportDate.DayOfWeek;
                    // create new tube
                    reportTube.ReportDay = day;
                    reportTube.TheaterId = item.TheaterId;
                    reportTube.TheaterName = item.TheaterName;
                    reportTube.ReportDate = item.ReportDate;
                    reportTube.SupplierName = item.SupplierName;
                    reportTube.NumberOfTicket = item.NumberOfTicket;
                    reportTube.TotalPurePrice = item.TotalPurePrice;
                    if (item.NumberOfSoldTicket > 0)
                    {
                        reportTube.NumberOfSoldTicket = item.NumberOfSoldTicket;
                        reportTube.TotalPrice = item.TotalPrice;
                        reportTube.Balance = item.Balance;
                    }
                    if (item.NumberOfReturnTicket > 0)
                    {
                        reportTube.NumberOfReturnTicket = item.NumberOfReturnTicket;
                    }
                    if (add == false) { add = true; }
                }
                // else, update old one
                else
                {
                    //update number of tiket and totalprice
                    reportTube.NumberOfTicket = reportTube.NumberOfTicket + item.NumberOfTicket;
                    reportTube.TotalPurePrice = reportTube.TotalPurePrice + item.TotalPurePrice;
                    if (item.NumberOfSoldTicket > 0)
                    {
                        reportTube.NumberOfSoldTicket = reportTube.NumberOfSoldTicket + item.NumberOfSoldTicket;
                        reportTube.TotalPrice = reportTube.TotalPrice + item.TotalPrice;
                        reportTube.Balance = reportTube.Balance + item.Balance;
                    }
                    if (item.NumberOfReturnTicket > 0)
                    {
                        reportTube.NumberOfReturnTicket = reportTube.NumberOfReturnTicket + item.NumberOfReturnTicket;
                    }
                    if (add == false) { add = true; }
                }
            }
            sortedData.Add(reportTube);
            List<ReportTube> fData = (from d in sortedData orderby d.ReportDate.DayOfWeek, d.TheaterName select d).ToList();
            return fData;
        }
        #endregion

        #region Get user information
        /// <summary>
        /// get username of current user
        /// </summary>
        /// <returns></returns>
        public string GetUserName()
        {
            string username = "a";
            if (this.Session["UserName"] != null)
            {
                username = this.Session["UserName"].ToString();
            }
            return username;
        }
        #endregion
    }
}
